package com.wontube.smr.dao.bo;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.dao.mapper.MovieMapper;
import com.wontube.smr.pub.DefaultDBHelper;
import com.wontube.smr.web.client.model.bo.QueryItemParams;

/**
 * MovieWeekDao Dao层
 * 
 * @author hewc@wondershare.cn
 * 
 */
public class MovieWeekDao {
	private MovieWeekDao() {
	}
	
	/**
	 * 今周最受欢迎音乐
	 * 
	 * @param pageInfo
	 * @return
	 */
	public static MovieItems selectMusics(String genreName, PagenateInfo pageInfo) {
		return selectMusics(genreName, null, pageInfo);
	}
	
	public static MovieItems selectMusics(QueryItemParams params, PagenateInfo pageInfo) {
		return selectMusics(params.getGenre(), params.getInitial(), pageInfo);
	}
	
	public static MovieItems selectMusics(String genreName,String initial, PagenateInfo pageInfo) {
		MovieItems items = new MovieItems(pageInfo);
		
		RowBounds rowBounds = new RowBounds(pageInfo.getOffset(), pageInfo.getLimit());
		QueryExample ex = new QueryExample();
		ex.or().addCriterion("movie.movie_type = ", Movie.TYPE_MUSIC)
			.addCriterion("movie.title_initial=", initial);
		ex.setOrderByClause("movie_week.sort_index asc");
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		
		try {			
			MovieMapper movieMapper = session.getMapper(MovieMapper.class);
			if (StringUtils.isNullOrEmpty(genreName)) {
				items.setItems(movieMapper.selectWithWeek(ex, rowBounds));
				if(pageInfo.getPageNo() == 1){
					items.setTotalCount(movieMapper.countWithWeek(ex));
				}
				
			} else {
				items.setItems(movieMapper.selectWithWeekByGenre(genreName, ex, rowBounds));
				if(pageInfo.getPageNo() == 1){
					items.setTotalCount(movieMapper.countWithWeekByGenre(genreName, ex));
				}
			}			
		} finally {
			session.close();
		}
		return items;
	}
}
